/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ozi.server;

import com.ozi.core.OZIMarshaller;
import com.ozi.core.SIFAck;
import com.ozi.core.SIFMessage;
import com.ozi.server.db.DBAccess;
import com.ozi.server.db.Zone;
import com.ozi.server.utils.Configurator;
import com.ozi.server.utils.Constants;
import com.ozi.utils.Codes;
import com.ozi.utils.IDGenerator;
import java.io.*;
import java.net.*;

import java.util.logging.Level;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

/**
 *
 * @author Matt
 */
public class oziZIS extends HttpServlet {

    private static Logger log = Logger.getLogger(DBAccess.class);
    private Configurator config = Configurator.createConfig();
    private static DBAccess db = DBAccess.getInstance();
    private Zone testZone = db.getZoneByName(Configurator.get(Constants.TEST_ZONE));
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception {
        // Message Marshaller
        OZIMarshaller marshaller = OZIMarshaller.getInstance();
        // Get path info for zone name request
        String pathInfo = request.getPathInfo();
        // Zone information
        Zone zone;
        // Requested zone
        String reqZone = Configurator.get(Constants.TEST_ZONE);
        if (pathInfo != null) {
            reqZone = pathInfo.substring(1);
        }
        // Setup output writer
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        // Build servletURL for zone creation
        URL servletURL = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), request.getRequestURI());
        // If test zone does not exist lets create it
        if (testZone.getZoneName().equals("")) {
            log.debug("We found: " + testZone.getZoneName() + " Looking for:" + Configurator.get(Constants.TEST_ZONE));
            db.createZone(Configurator.get(Constants.TEST_ZONE), servletURL.toString() + "/" + Configurator.get(Constants.TEST_ZONE));
            testZone = db.getZoneByName(Configurator.get(Constants.TEST_ZONE));
        }
        // check if zone requested exists in the database
        if (db.getZoneByName(reqZone).getZoneName().equals(reqZone)) {
            zone = db.getZoneByName(reqZone);
        } else {
            // TODO: have this throw a SIFAck with invalid zone information
            zone = testZone;
        }
        out.print(zone.getZoneName().toString());
        out.close();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(oziZIS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(oziZIS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
